package top.mschen.blog.common.exception;

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

@Configuration
//@Component
public class ErrorConfig implements ErrorPageRegistrar {

//     @Autowired
//    private ApplicationContext applicationContext;

    /**
     * 2.0
     * @param registry
     */
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {

        /**
         *使用Binder不需要樽寻spring 属性
         */
//        Binder binder = Binder.get(applicationContext.getEnvironment());
        ErrorPage[] errorPages = new ErrorPage[1];
//        errorPages[0] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");
        errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");
        registry.addErrorPages(errorPages);
    }

//    //统一页码处理配置 1.5.x
//    @Bean
//    public EmbeddedServletContainerCustomizer containerCustomizer() {
//        return new EmbeddedServletContainerCustomizer() {
//            @Override
//            public void customize(ConfigurableEmbeddedServletContainer container) {
//                //ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
//                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/Err404.html");
//                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/Err500.html");
//
//                container.addErrorPages( error404Page, error500Page);
//            }
//        };
//    }


}
